#include <iostream>
using namespace std;
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>

int main()
{
    const char * fileName = "./tongxin1.txt";
    int fd = open(fileName,  O_RDWR);
    if(fd == -1)
    {
        perror("open error:");
        exit(-1);
    }
    /* 获取文件大小 */
    off_t length = lseek(fd, 0, SEEK_END);  //length不能为零，为零会导致mmap失败，允许映射空间大小

    void * address = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(address == MAP_FAILED)
    {
        perror("mmap error:");
        exit(-1);
    }

    /* 关闭文件描述符 */
    close(fd);

    cout << "data:" << (char*)address << endl;

    /* 释放共享内存 */
    munmap(address, length);

    return 0;
}